package lx.utils.http.TextRequest;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

public class DataHull {

    public enum DataStatus {
        STATUS_NOT_INIT,
        STATUS_OK,

        ERR_NO_NET_WORK,
        ERR_CONNECT_FAIL,
        ERR_REQUEST_FAIL,
        ERR_DATA_NULL,
        ERR_PARSE_FAIL,
        ERR_WRONG_URL,
        ERR_UNKNOWN,
    }

    public enum DataSource {
        UNKNOWN,
        FROM_SERVER,
        FROM_MEMORY_CACHE,
        FROM_SDCARD_CACHE,
    }

    private int mHttpStatusCode;
    private DataStatus mDataStatusCode;
    private DataSource mDataSource;
    private String mRequestUrl;
    private URL mRequestURL;
    private URI mRequestURI;
    private String mOriginData;
    private Object mParsedData;

    DataHull() {
        mDataStatusCode = DataStatus.STATUS_NOT_INIT;
        mDataSource = DataSource.UNKNOWN;
    }

    DataHull(String url) {
        this();
        mRequestUrl = url;
    }

    public int getHttpStatusCode() {
        return mHttpStatusCode;
    }

    public DataStatus getDataStatusCode() {
        return mDataStatusCode;
    }

    public DataSource getDataSource() {
        return mDataSource;
    }

    public String getRequestUrlString() {
        return mRequestUrl;
    }

    public URL getRequestURL() {
        if (mRequestURL == null) {
            try {
                mRequestURL = new URL(mRequestUrl);
            } catch (MalformedURLException e) {
                mDataStatusCode = DataStatus.ERR_WRONG_URL;
                e.printStackTrace();
            }
        }
        return mRequestURL;
    }

    public URI getRequestURI() {
        if (mRequestURI == null) {
            if (getRequestURL() != null) {
                try {
                    mRequestURI = mRequestURL.toURI();
                } catch (URISyntaxException e) {
                    mDataStatusCode = DataStatus.ERR_WRONG_URL;
                    e.printStackTrace();
                }
            }
        }
        return mRequestURI;
    }

    public String getOriginData() {
        return mOriginData;
    }

    public Object getParsedData() {
        return mParsedData;
    }

    void setParsedData(Object data) {
        mParsedData = data;
    }

    void setRequestUrl(String url) {
        mRequestUrl = url;
    }

    void setDataStatusCode(DataStatus status) {
        mDataStatusCode = status;
    }

    void setHttpStatusCode(int status) {
        mHttpStatusCode = status;
    }

    void setDataSource(DataSource dataSource) {
        mDataSource = dataSource;
    }

    void setOriginData(String data) {
        if (data == null) {
            mDataStatusCode = DataStatus.ERR_DATA_NULL;
        } else {
            mOriginData = data;
        }
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("request url : ").append(getRequestUrlString())
                .append("\nhttp status code : ").append(getHttpStatusCode())
                .append("\ndata status code : ").append(getDataStatusCode())
                .append("\ndata source : ").append(getDataSource())
                .append("\norigin data : ").append(getOriginData())
                .append("\nparsed data : ").append(getParsedData());
        return sb.toString();
    }
}
